<?php

namespace app\channel\controller\api;

use app\channel\ApiController;
use app\mainadmin\model\PaymentModel;


use app\channel\model\BondModel;
use think\Db;

/*------------------------------------------------------ */
//-- 保证金明细
/*------------------------------------------------------ */

class Bond extends ApiController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new BondModel();
    }
    /*------------------------------------------------------ */
    //-- 保证金统计
    /*------------------------------------------------------ */
    public function headerTotal()
    {
        $post = request()->param();
        $where['user_id'] = $this->userInfo['user_id'];
        $where['type'] = $post['type'];
        $data_info = Db::name('channel_proxy_bond')->where($where)->field('amount_paid,amount_returned,amount_be_returned,frozen_amount')->find();
        if (!$data_info) {
            Db::name('channel_proxy_bond')->insert(array('user_id' => $this->userInfo['user_id'], 'type' => 1));
            Db::name('channel_proxy_bond')->insert(array('user_id' => $this->userInfo['user_id'], 'type' => 2));
            $data_info = Db::name('channel_proxy_bond')->where($where)->field('amount_paid,amount_returned,amount_be_returned,frozen_amount')->find();

        }
        if ($data_info['amount_paid'] >= 1000) $data_info['amount_paid'] = ($data_info['amount_paid'] / 1000) . 'K';
        if ($data_info['amount_returned'] >= 1000) $data_info['amount_paid'] = ($data_info['amount_returned'] / 1000) . 'K';
        if ($data_info['amount_returned'] >= 1000) $data_info['amount_paid'] = ($data_info['amount_returned'] / 1000) . 'K';
        if ($data_info['amount_be_returned'] >= 1000) $data_info['amount_paid'] = ($data_info['amount_be_returned'] / 1000) . 'K';
        return $this->success($data_info);
    }
    /*------------------------------------------------------ */
    //-- 保证金明细
    /*------------------------------------------------------ */
    public function getList()
    {
        $type = input('type', '0', 'intval');
        $BondModel = new BondModel();
        if ($type==2){
            $where[] = ['proxy_pid', '=', $this->userInfo['user_id']];
        }else{
            $where[] = ['user_id', '=', $this->userInfo['user_id']];
            $where[] = ['type', '=', $type];
        }

        $data = $this->getPageList($BondModel, $where, '', 10);
        foreach ($data['list'] as $k => $v) {
            if ($v['order_id'] > 0) {
                if ($v['status'] != 2) {
                    //查询订单
                    $data['list'][$k]['is_goods_order'] = 1;
                    $data['list'][$k]['goods_order'] = Db::name('channel_order_goods')->where(array('order_id' => $v['order_id']))->field('goods_name,goods_number,balance_deduction')->select();
                }
            } else {
                $data['list'][$k]['is_goods_order'] = 0;
                $data['list'][$k]['goods_order'] = array();
            }
        }
        return $this->success($data);

    }

    /*------------------------------------------------------ */
    //-- 保证金明细
    /*------------------------------------------------------ */
    public function getPayOrder()
    {

        $order_id = input('order_id', '0', 'intval');
        $type = input('type', '0', 'intval'); //1-保证金退回;2下级奖励保证金
        if ($type == 1) {
            $where_order_info['order_id'] = $order_id;
            $where_order_info['supply_uid'] = $this->userInfo['user_id'];
            $data = Db::name('channel_order_info')->where($where_order_info)->field('order_id,user_id,balance_deduction')->find();
        } else if ($type == 2) {
            $data = Db::name('channel_reward_log')->where(array('id' => $order_id))->field('to_uid as user_id,reward_money balance_deduction')->find();
        } else {
            return $this->error("参数有误");
        }
        //获取用户账号
        $data_user_info = Db::name('users')->where(array('user_id' => $data['user_id']))->find();
        $platform = input('platform', '', 'trim');
        $payList = (new PaymentModel)->getRows($platform);
        $data['payList'] = [];
        $isOffLinePay = 0;
        foreach ($payList as $pay) {
            if ($pay['pay_code'] == 'offline') {
                $isOffLinePay = 1;
            } else {
                continue;

            }
            $_pay['pay_id'] = $pay['pay_id'];
            $_pay['pay_code'] = $pay['pay_code'];
            $_pay['pay_name'] = $pay['pay_name'];
            $data['payList'][] = $_pay;
        }

        //获取下级打款账号
//        Db::name('users')->find();

        if ($isOffLinePay == 1) {
            $data['offline']['is_sys'] = 1;
            $data['offline']['weixin_usd'] = empty($data_user_info['weixin_qrcode']) ? 0 : 1;
            $data['offline']['alipay_usd'] = empty($data_user_info['alipay_qrcode']) ? 0 : 1;
            $data['offline']['bank_usd'] = empty($data_user_info['bank_name']) ? 0 : 1;
            //微信
            if ($data['offline']['weixin_usd'] == 1) {
                $data['offline']['is_usd'] = 1;
                $data['offline']['weixin_qrcode'] = $data_user_info['weixin_qrcode'];
                $data['offline']['weixin_name'] = $data_user_info['weixin_name'];
                $data['offline']['weixin_account'] = $data_user_info['weixin_account'];
            }
            //支付宝
            if ($data['offline']['alipay_usd'] == 1) {
                $data['offline']['is_usd'] = 1;
                $data['offline']['alipay_qrcode'] = $data_user_info['alipay_qrcode'];
                $data['offline']['alipay_name'] = $data_user_info['alipay_name'];
                $data['offline']['alipay_account'] = $data_user_info['alipay_account'];
            }
            //银行卡
            if ($data['offline']['bank_usd'] == 1) {
                $data['offline']['is_usd'] = 1;
                //开会银行
                $data['offline']['bank_name'] = $data_user_info['bank_name'];
                //开户行
                $data['offline']['bank_subbranch'] = $data_user_info['bank_subbranch'];
                //持卡人姓名
                $data['offline']['bank_user_name'] = $data_user_info['bank_user_name'];
                //银行卡号
                $data['offline']['bank_card_number'] = $data_user_info['bank_card_number'];
            }
        }
        return $this->success($data);
    }

}

